package com.sky.JULLog;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.logging.LogManager;
import java.util.logging.Logger;

/**
 * @author : wushikai
 * <p>
 * date : 2022-05-18
 */
public class MyLogger {

    private static Logger log;


    static {


        LogManager logManager = LogManager.getLogManager();   //  创建logManager,logManager是单例模式,因此不能new,去获取即可

        // 通过LogManager加载配置文件,会完成Logger的初始化,即将配置文件中的Logger(包括顶级Logger和我们自定义的Logger)添加到LogManager中,保存在HashTable中(即map集合).
        String file = Objects.requireNonNull(MyLogger.class.getClassLoader().getResource("JUL.properties")).getFile();
        try {
            logManager.readConfiguration(new FileInputStream(file));
            String formatterClazzPath = logManager.getProperty("java.util.logging.ConsoleHandler.formatter");
            Class.forName(formatterClazzPath);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.err.println("你没在 JUL.properties 文件中配置好 ConsoleHandler.formatter! ");

        }

    }


    public static Logger getLogger(Class c) {

        return  Logger.getLogger(c.getName());
    }

    public static  Logger getLogger( String  c){
        return  Logger.getLogger(c);
    }

}
